/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package aptech.eproject.logictics.dao.khoa;

import aptech.eproject.logictics.db.*;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.*;
import javax.annotation.*;
import javax.ejb.*;
import javax.persistence.*;
/**
 *
 * @author zZzShaRazZz
 */
@Stateless
@TransactionManagement(value = TransactionManagementType.CONTAINER)
public class Register implements RegisterRemote {

    @Resource
    private SessionContext context;
    @PersistenceContext(name = "LogicticsService")
    EntityManager em;

    @TransactionAttribute(TransactionAttributeType.REQUIRED)
    public boolean addCustomer(String username, String password, String fullname, Timestamp birthday, String gender, String address, int cityid, String phonenumber, String email) {
        try {
            Customer customer = new Customer();
            customer.setUserName(username);
            customer.setPassword(password);
            customer.setFullName(fullname);
            customer.setDateOfBirth(birthday);
            customer.setGender(gender);
            customer.setAddress(address);
            City city = em.find(City.class, new Integer(cityid));
            customer.setCity(city);
            customer.setPhoneNumber(phonenumber);
            customer.setEmail(email);
            em.persist(customer);
            return true;
        } catch (Exception ex) {
            ex.printStackTrace();
            return false;
        }
    }

    public boolean isUserNameExisted(String username) {
        Query query1 = em.createQuery("SELECT c FROM Customer c WHERE c.userName=?1");
        query1.setParameter("1", username);
        Query query2 = em.createQuery("SELECT a FROM Account a WHERE a.userName=?1");
        query2.setParameter("1", username);
        List q1 = query1.getResultList();
        List q2 = query2.getResultList();
        if (q1.isEmpty() && q2.isEmpty()) {
            return false;
        }
        return true;
    }

    public boolean isEmailExisted(String email) {
        Query query1 = em.createQuery("SELECT c FROM Customer c WHERE c.email=?1");
        query1.setParameter("1", email);
        Query query2 = em.createQuery("SELECT a FROM Account a WHERE a.email=?1");
        query2.setParameter("1", email);
        List q1 = query1.getResultList();
        List q2 = query2.getResultList();
        if (q1.isEmpty() && q2.isEmpty()) {
            return false;
        }
        return true;
    }

    public List getBranchByCityID(int cityid) {
        Query query = em.createQuery("SELECT b FROM Branch b WHERE b.isDeleted='False' AND b.city.cityId=?1");
        query.setParameter("1", cityid);
        return query.getResultList();
    }

    public List getCity() {
        Query query = em.createQuery("SELECT c FROM City c WHERE c.isDeleted='False'");
        return query.getResultList();
    }

    // Add business logic below. (Right-click in editor and choose
    // "Insert Code > Add Business Method")
 
}
